<?php
declare (strict_types = 1);

namespace app\admin\controller\erp;

use app\admin\controller\AuthController;
use app\admin\model\erp\ErpVoucher;
use think\facade\Db;
use think\Request;

class Finance extends AuthController
{
    /**
     * 科目余额表
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function index(Request $req)
    {
        $start_day = $req->param('start_at', date('Y-m-01'));
        $end_day = $req->param('end_at', date('Y-m-d'));
        if($req->isAjax()) {
            $start_at = strtotime($start_day);
            $end_at = strtotime($end_day.' 23:59:59');
            //期末
            $end_list = Db::name('erp_subjects')->alias('a')
                ->leftJoin('erp_voucher b', "b.period_at <= '$end_at' AND b.code LIKE CONCAT(a.code,'%')")
                ->group('a.code')
                ->order('a.level ASC,a.id ASC')
                ->field('a.id,a.pid,a.code,a.name,a.pname,a.level,ifnull(ROUND(SUM(b.debit),2),0.00) as end_debit,ifnull(ROUND(SUM(b.credit),2), 0.00) as end_credit')->select()->toArray();
            //echo Db::getLastSql();
            $end_data = [];
            $end_debit = Db::name('erp_voucher')->where('period_at','<=',$end_at)->sum('debit');
            $end_credit = Db::name('erp_voucher')->where('period_at','<=',$end_at)->sum('credit');
            foreach($end_list as $val) {
                $end_data[$val['id']] = $val;
            }
            //期间
            $list = Db::name('erp_subjects')->alias('a')
                ->leftJoin('erp_voucher b', "b.period_at >= '$start_at' AND b.period_at <= '$end_at' AND b.code LIKE CONCAT(a.code,'%')")
                ->group('a.code')
                ->order('a.level ASC,a.id ASC')
                ->field('a.id,a.pid,a.code,a.name,a.pname,a.level,ifnull(ROUND(SUM(b.debit),2),0.00) as debit,ifnull(ROUND(SUM(b.credit),2), 0.00) as credit')->select()->toArray();
            $data = [];
            $debit = Db::name('erp_voucher')->where('period_at','>=',$start_at)->where('period_at','<=',$end_at)->sum('debit');
            $credit = Db::name('erp_voucher')->where('period_at','>=',$start_at)->where('period_at','<=',$end_at)->sum('credit');
            foreach($list as $val) {
                $data[$val['id']] = $val;
            }
            //期初
            $start_list = Db::name('erp_subjects')->alias('a')
                ->leftJoin('erp_voucher b', "b.period_at < '$start_at' AND b.code LIKE CONCAT(a.code,'%')")
                ->group('a.code')
                ->order('a.level ASC,a.id ASC')
                ->field('a.id,a.pid,a.code,a.name,a.pname,a.level,ifnull(ROUND(SUM(b.debit),2),0.00) as start_debit,ifnull(ROUND(SUM(b.credit),2), 0.00) as start_credit')->select()->toArray();

            $start_data = [];
            $start_debit = Db::name('erp_voucher')->where('period_at','<',$start_at)->sum('debit');
            $start_credit = Db::name('erp_voucher')->where('period_at','<',$start_at)->sum('credit');
            $start_data['0'] = ['id'=>'a','pid'=>0,'name'=>'','code'=>'合计：','start_debit'=>round($start_debit,2),'start_credit'=>round($start_credit,2),'debit'=>round($debit,2),'credit'=>round($credit,2),'end_debit'=>round($end_debit,2),'end_credit'=>round($end_credit,2)];
            foreach($start_list as $val) {
                $val = array_merge($val, $data[$val['id']],$end_data[$val['id']]);
                $start_data[$val['id']] = $val;
            }
            return json($start_data);
        }
        $this->assign('start_day', $start_day);
        $this->assign('end_day', $end_day);
        return $this->fetch();
    }

    /**
     * 显示凭证
     *
     * @param  \think\Request  $req
     * @return \think\Response
     */
    public function show(Request $req)
    {
        if ($req->isAjax()) {
            $data = [];
            $period_code = $req->param('period_code/d', '0');
            $query = ErpVoucher::where('period_code','=',$period_code)->order('position ASC');
            $data['count'] = $query->count();
            $data['data'] = $query->select()->toArray();
            //echo $query->getLastSql();die;
            $data['code'] = 0;
            $data['msg'] = '';
            foreach ($data['data'] as &$val) {
                $val['period_at'] = $val['period_at'] ? date('Y-m-d',$val['period_at']) : '--/--';
                $val['debit'] = $val['debit'] ? number_format($val['debit'],2,'.','') : '';
                $val['credit'] = $val['credit'] ? number_format($val['credit'],2,'.','') : '';
            }
            return json($data);
        }
        $code = $req->param('code','');
        $start_at = $req->param('start_at', date('Y-m-01'));
        $end_at = $req->param('end_at', date('Y-m-d'));
        $start = strtotime($start_at);
        $end = strtotime($end_at.' 23:59:59');
        $query = Db::name('erp_voucher')->alias('a')
            ->leftJoin('erp_voucher_period b', 'b.period_code = a.period_code')
            ->field('a.*,b.description,b.market')
            ->where('a.period_at', '>=', $start)
            ->where('a.period_at', '<=', $end);
        if(!$code) {
            $query->where('a.code', 'like', $code . '%');
        }
        $item = $query->select()->toArray();
        //echo $query->getLastSql();
        foreach ($item as &$val) {
            $val['debit'] = $val['debit'] ? number_format($val['debit'],2,'.','') : '0';
            $val['credit'] = $val['credit'] ? number_format($val['credit'],2,'.','') : '0';
        }
        $this->assign('item', $item);
        return $this->fetch();
    }
}
